﻿Imports System.Threading
Imports System.Net.NetworkInformation
Imports System.Management

Public Class NICPoller

    Shared Event NicChange(sender As Object, e As NICPollEventArgs)
    Shared Event BatteryLevel(sender As Object, e As BatteryPollEventArgs)

    Private Const SECONDS As Integer = 1000
    Private Const MINUTES As Integer = 60000

    Private Shared pollTimerNIC As Timer
    Private Shared pollTimerBattery As Timer
    Private Shared isConnected As Boolean
    Private Shared isChecking As Boolean

    Shared Sub New()
        pollTimerNIC = New Timer(AddressOf CheckNics, Nothing, 0, SECONDS * 1)
        pollTimerBattery = New Timer(AddressOf CheckBattery, Nothing, 0, MINUTES * 1)

    End Sub

    Private Shared Sub CheckNics(state As Object)
        Dim nics() As NetworkInterface

        If isChecking Then Exit Sub

        Try
            isChecking = True

            nics = NetworkInterface.GetAllNetworkInterfaces
            For Each nic As NetworkInterface In nics
                If nic.NetworkInterfaceType = NetworkInterfaceType.Ethernet And nic.Speed <> 3000000 Then
                    If Not isConnected Then RaiseEvent NicChange(Nothing, New NICPollEventArgs(True))
                    isConnected = True
                    Exit Sub
                End If
            Next

            If isConnected Then RaiseEvent NicChange(Nothing, New NICPollEventArgs(False))
            isConnected = False

        Catch ex As Exception
            ' Default to connected state and show screen
            If Not isConnected Then RaiseEvent NicChange(Nothing, New NICPollEventArgs(True))
            isConnected = True

        Finally
            isChecking = False
        End Try

    End Sub

    Private Shared Sub CheckBattery(state As Object)
        Dim search As ManagementObjectSearcher

        Try
            search = New ManagementObjectSearcher("select * from Win32_Battery")

            For Each mo As ManagementObject In search.Get
                RaiseEvent BatteryLevel(Nothing, New BatteryPollEventArgs(CType(mo.Properties("BatteryStatus").Value, BatteryPollEventArgs.BatteryStatuses), CInt(mo.Properties("EstimatedChargeRemaining").Value)))
                Exit For
            Next

        Catch ex As Exception

        End Try

    End Sub

    Public Class NICPollEventArgs
        Inherits EventArgs

        Public IsConnected As Boolean

        Public Sub New()

        End Sub

        Public Sub New(isConnected As Boolean)
            Me.IsConnected = isConnected
        End Sub

    End Class

    Public Class BatteryPollEventArgs
        Inherits EventArgs

        Public Enum BatteryStatuses
            NotSet = 0
            Discharging = 1
            OnACPower = 2
            FullyChaged = 3
            LowBattery = 4
            CriticallyLowBattery = 5
            Charging = 6
            ChargingHigh = 7
            ChargingLow = 8
            Undefined = 9
            PartiallyCharged = 10
        End Enum

        Public EstimatedChargeRemaining As Integer
        Public BatteryStatus As BatteryStatuses

        Public Sub New()

        End Sub

        Public Sub New(status As BatteryStatuses, chargeRemaining As Integer)
            Me.BatteryStatus = status
            Me.EstimatedChargeRemaining = chargeRemaining
        End Sub

    End Class

End Class
